package com.scau.classOnline.controller;

import com.scau.classOnline.common.CommentAlter;
import com.scau.classOnline.common.UserUtils;
import com.scau.classOnline.entity.Comment;
import com.scau.classOnline.model.RespEntity;
import com.scau.classOnline.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api/comment")
public class CommentController {

    @Autowired
    private CommentService commentServer;

    @PostMapping(value = "/putComment", produces = MediaType.APPLICATION_JSON_VALUE)
    public RespEntity putComment(Integer cid, Integer stars, String content) {
        int id = UserUtils.getCurrentUser().getId();
        int i = commentServer.addComment(id, cid, stars, content);
        if (i == 1) {
            return RespEntity.ok("评论成功！");
        } else if (i == -1) {
            return RespEntity.error("你已评论，请勿重复评论！");
        } else {
            return RespEntity.error("评论失败！");
        }
    }

    @GetMapping(value = "/getComments")
    public RespEntity getComments(Integer cid, Integer offset) {
        List<Comment> comments = commentServer.getComments(cid, offset, CommentAlter.TOTAL.getCode());
        return RespEntity.ok("查询评论成功！", comments);
    }

    @GetMapping(value = "/getGoodComments")
    public RespEntity getGoodComments(Integer cid, Integer offset) {
        List<Comment> comments = commentServer.getComments(cid, offset, CommentAlter.GOOD.getCode());
        return RespEntity.ok("查询评论成功", comments);
    }

    @GetMapping(value = "/getBadComments")
    public RespEntity getBadComments(Integer cid, Integer offset) {
        List<Comment> comments = commentServer.getComments(cid, offset, CommentAlter.BAD.getCode());
        return RespEntity.ok("查询评论成功", comments);
    }

    @GetMapping(value = "/computeFavRate")
    public RespEntity computeFavRate(Integer cid) {
        double favRate = commentServer.computeFavRate(cid);
        return RespEntity.ok("success", Double.toString(favRate));
    }
}
